home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-01-13 | 1.5 KB | 66 lines | [TEXT/CWIE] |
- /*
- File: SortedDynamicArray.cp
-
- Contains: An abstract base class for sorted dynamic arrays
-
- Written by: Dave Falkenburg
-
- Copyright: © 1994-95 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
- <1> 1/3/95 DRF First checked in.
- */
-
- #include "SortedDynamicArray.h"
-
- //--------------------------------------------------------------------------------
- TSortedDynamicArray::TSortedDynamicArray()
- {
- }
-
-
- //--------------------------------------------------------------------------------
- // An O(n) (insertion sort) routine for adding elements
- OSErr TSortedDynamicArray::Add(ArrayElementPtr newElement)
- {
- ArrayElementIndex index;
-
- for (index = 0;index < fElementCount;index++)
- {
- if (this->Compare(newElement,(*fStorage)[index]) > 0)
- continue;
-
- return this->Insert(newElement,index);
- }
-
- return this->InsertLast(newElement);
- }
-
-
- //--------------------------------------------------------------------------------
- // A O(lg n) (binary search) routine for finding elements
- ArrayElementPtr TSortedDynamicArray::Find(ArrayElementPtr prototypeElement)
- {
- ArrayElementIndex low,high,index;
- long value;
-
- low = 0;
- high = fElementCount;
-
- while (low < high)
- {
- index = (low+high)/2;
- value = this->Compare(prototypeElement,(*fStorage)[index]);
-
- if (value < 0)
- high = index; // element is below "high"
- else if (value == 0)
- return (*fStorage)[index]; // found the element
- else
- low = index+1; // element is above "low"
- }
-
- return NULL;
- }
-